import numpy as np
import matplotlib.pyplot as plt

# 假设已经定义了 production_cost 函数
def production_cost(N, a11, a12, a13, a21, a22, a31, a32, a33, c1, c2, c3, c4, detect_part1, detect_part2, detect_final, disassemble):
    """
    计算生产成本的函数。

    参数:
    N : int
        生产数量
    a11, a12, a13 : float
        零件不合格率相关参数
    a21, a22 : float
        零件采购成本相关参数
    a31, a32, a33 : float
        零件检测成本相关参数
    c1, c2, c3, c4 : float
        成品相关成本参数
    detect_part1, detect_part2 : float
        零件检测比例
    detect_final : float
        成品检测比例
    disassemble : float
        不合格成品的拆解比例

    返回:
    total_cost : float
        总成本
    """

    # 零件1、2的合格率
    part_quality_rate1 = 1 - a11 * (1 - detect_part1)
    part_quality_rate2 = 1 - a12 * (1 - detect_part2)

    # 成品的合格率
    product_quality_rate = part_quality_rate1 * part_quality_rate2 * (1 - a13 * (1 - detect_final))

    # 零件1、2的采购成本
    part_purchase_cost1 = N / part_quality_rate1 * a21
    part_purchase_cost2 = N / part_quality_rate2 * a22

    # 零件1、2的检测成本
    part_testing_cost1 = detect_part1 * N * a31
    part_testing_cost2 = detect_part2 * N * a32

    # 成品的装配成本
    assembly_cost = N * c4

    # 成品的检测成本
    final_testing_cost = detect_final * N * a33

    # 不合格品的调换成本
    replacement_cost = (1 - detect_final) * N * (1 - product_quality_rate) * c2

    # 不合格成品的拆解成本和回收收益
    scrap_cost = disassemble * N * (1 - product_quality_rate) * c3
    scrap_revenue = disassemble * N * (1 - product_quality_rate) * 0.6 * c1

    # 总成本计算
    total_cost = (part_purchase_cost1 + part_purchase_cost2 +
                  part_testing_cost1 + part_testing_cost2 +
                  assembly_cost + final_testing_cost +
                  replacement_cost + scrap_cost - scrap_revenue)

    return total_cost
# 初始化参数
N = 1000  # 生产的产品数量
a11 = [0.1, 0.2, 0.1, 0.2, 0.1, 0.05]  # 零配件1次品率
a21 = [4, 4, 4, 4, 4, 4]  # 零配件1单价
a31 = [2, 2, 2, 1, 8, 2]  # 零配件1检测成本
a12 = [0.1, 0.2, 0.1, 0.2, 0.2, 0.05]  # 零配件2次品率
a22 = [18, 18, 18, 18, 18, 18]  # 零配件2单价
a32 = [3, 3, 3, 1, 1, 3]  # 零配件2检测成本
a13 = [0.1, 0.2, 0.1, 0.2, 0.1, 0.05]  # 成品次品率
c4 = [6, 6, 6, 6, 6, 6]  # 成品装配成本
a33 = [3, 3, 3, 2, 2, 3]  # 成品检测成本
c1 = [56, 56, 56, 56, 56, 56]  # 成品市场售价
c2 = [6, 6, 30, 30, 10, 10]  # 不合格品调换损失
c3 = [5, 5, 5, 5, 5, 40]  # 拆解费用

# 用于存储总成本和最优方案编号
data = np.zeros((6, 17))
# 用于存储每种情况下的最小总成本
optimal_costs = np.zeros(6)

# 遍历所有可能的决策组合
for i in range(6):
    minvalue = float('inf')
    minlabel = 0
    number = 1

    for detect_part1 in range(2):  # 是否检测零配件1
        for detect_part2 in range(2):  # 是否检测零配件2
            for detect_final in range(2):  # 是否检测成品
                for disassemble in range(2):  # 是否拆解不合格成品
                    # 计算总成本
                    total_cost = production_cost(N, a11[i], a12[i], a13[i], a21[i], a22[i], a31[i], a32[i], a33[i],
                                                 c1[i], c2[i], c3[i], c4[i], detect_part1, detect_part2, detect_final,
                                                 disassemble)
                    data[i, number - 1] = total_cost

                    # 找出最小成本
                    if total_cost < minvalue:
                        minvalue = total_cost
                        minlabel = number
                    number += 1

    # 存储最优方案编号和最小成本
    data[i, 16] = minlabel
    optimal_costs[i] = minvalue

# 可视化每种情况下的最优方案总成本
plt.figure()
plt.bar(range(6), optimal_costs)
plt.title('每种情况下的最优总成本')
plt.xlabel('情景编号')
plt.ylabel('最小总成本')
plt.xticks(range(6))
plt.grid(True)

# 可视化每种情况下的所有方案成本对比
plt.figure()
colors = plt.cm.viridis(np.linspace(0, 1, 6))
for i in range(6):
    plt.plot(range(16), data[i, :16], 'o-', color=colors[i], label=f'情景 {i + 1}')
plt.title('不同情景下各方案总成本对比')
plt.xlabel('方案编号')
plt.ylabel('总成本')
plt.legend()
plt.grid(True)
plt.show()